/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.app.content;

import han.bifang.gwt.client.app.AppContentConstants;
import han.bifang.gwt.client.app.AppMessages;
import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Application;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateOrModifyOrRemoveAppContent extends Content {
	public static final String CREATE = "create";
	public static final String MODIFY = "modify";
	public static final String REMOVE = "remove";

	private AppMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private VerticalPanel panel = null;

	private Application app;

	private String action = null;

	private TextBox name = null;
	private TextBox code = null;
	
	private TextArea desc = null;

	private AsyncCallback<ResponseObject> createcb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			ArrayList a = (ArrayList) res.getResponse();
			String treeId = (String) a.get(0);
			String dId = (String) a.get(1);
			app.setId(dId);
			app.setUnitId(treeId);
			HashMap p = new HashMap();
			p.put("app", app);
			listener.onContentChanged(AppContentConstants.CREATE_APP,
					AppContentConstants.APP_ROOT, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};

	private AsyncCallback<ResponseObject> modifycb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			HashMap p = new HashMap();
			p.put("app", app);
			listener.onContentChanged(AppContentConstants.MODIFY_APP,
					AppContentConstants.APP_DETAIL, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};

	private AsyncCallback<ResponseObject> removecb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			HashMap p = new HashMap();
			p.put("app", app);
			listener.onContentChanged(AppContentConstants.REMOVE_APP,
					AppContentConstants.APP_ROOT, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};

	public CreateOrModifyOrRemoveAppContent(AppMessages cons,
			BifangGWTServiceAsync sservice, GwtContext context, HashMap params) {

		this.cons = cons;
		this.service = sservice;
		this.context = context;
		this.action = (String) params.get("action");
		this.app = (Application) params.get("app");

		panel = new VerticalPanel();
		panel.setWidth("100%");

		Button c = null;

		Grid table = new Grid(3, 2);
		table.setWidth("80%");
		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		table.setHTML(0, 0, cons.name());
		table.setHTML(1, 0, cons.code());
		table.setHTML(2, 0, cons.desc());

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");

		if (action.equals(CREATE)) {
			ff.setStyleName(0, 0, "tablecol");
			name = new TextBox();

			name.setVisibleLength(32);
			
			code = new TextBox();
			code.setVisibleLength(32);

			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);

			table.setWidget(0, 1, name);
			table.setWidget(1, 1, code);

			table.setWidget(2, 1, desc);

			c = new Button(cons.create());
			c.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					if (!validate()) {
						return;
					}
					app = new Application();

					
					app.setName(name.getText());
					app.setCode(code.getText());
					app.setDescription(desc.getText());
					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.app.CreateAppAction");
					req.setRequest(app);
					service.call(req, createcb);
				}
			});
			HTML h = new HTML(cons.appBasic());
			panel.add(h);
			panel.add(table);
			panel.add(c);
		}

		else if (action.equals(MODIFY)) {

			name = new TextBox();
			name.setVisibleLength(32);
			name.setText(app.getName());
			
			code = new TextBox();
			code.setVisibleLength(32);
			code.setText(app.getCode());

			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);
			desc.setText(app.getDescription());

			table.setWidget(0, 1, name);
			table.setWidget(1, 1, code);
			
			table.setWidget(2, 1, desc);

			c = new Button(cons.modify());
			c.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					if (!validate()) {
						return;
					}

					app.setName(name.getText());
					app.setCode(code.getText());
					app.setDescription(desc.getText());
					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.app.ModifyAppAction");
					req.setRequest(app);
					service.call(req, modifycb);
				}
			});
			HTML h = new HTML(cons.appBasic());
			panel.add(h);
			panel.add(table);
			panel.add(c);
		}

		else if (action.equals(REMOVE)) {

			table.setHTML(0, 1, app.getName());
			table.setHTML(1, 1, app.getCode());
			
			table.setHTML(2, 1, app.getDescription());

			c = new Button(cons.remove());
			c.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {

					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.app.RemoveAppAction");
					req.setRequest(app.getId());
					service.call(req, removecb);
				}
			});

			Button cancel = new Button(cons.cancel());
			cancel.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap p = new HashMap();

					listener.onContentChanged(
							AppContentConstants.REMOVE_APP,
							AppContentConstants.APP_DETAIL, p);

				}
			});

			HTML h = new HTML(cons.appBasic());
			panel.add(h);
			panel.add(table);
			HTML l = new HTML(cons.confirmRemove());
			panel.add(l);
			HorizontalPanel hp = new HorizontalPanel();
			hp.add(c);
			hp.add(cancel);
			panel.add(hp);
		}

		initWidget(panel);

	}
	
	private boolean validate() {
		if (!StringUtils.checkLenght(name.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.name(), "1", "128"));
			return false;
		}
		if (!StringUtils.checkLenght(code.getText(), 1, 32)) {
			Window.alert(cons.lengthError(cons.code(), "1", "32"));
			return false;
		}
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}
		return true;

	}

}
